<?php

namespace app\controller\api;
use plugin\admin\app\model\BusinessListing;
use support\Request;
use support\Response;

class MemberController extends BaseController
{
    /**
     * 新建养老院及更新信息
     * @return void
     */
    public function createOrder(Request $request){
        // 获取请求中的数据
        $data = $request->post();
        // 准备要插入的数据
        $add = array(
            'name' => $data['name'],
            'address' => $data['address'],
            'phone' => $data['phone'],
            'opening_time' => $data['opening_time'],
            'closing_time' => $data['closing_time'],
            'logo' => $data['logo'],
            'description' => $data['description'],
            'cover' => $data['cover'],
            'merchant_image' => $data['merchant_image'],
            'class_id' => $data['class_id'],
            'longitude' => $data['longitude'],
            'latitude' => $data['latitude'],
            'created_at' => date("Y-m-d H:i:s"),
            'updated_at' => date("Y-m-d H:i:s"),
            'is_settled' => 1,
        );
        if (isset($data['id']) && $data['id']){ //修改
            unset($data['is_settled']);
            unset($data['created_at']);
            $info = BusinessListing::where(['id'=>$data['id']])->first();
            if (!$info){
                return Response::faild("商家不存在！");
            }
            $res = BusinessListing::where(['id'=>$data['id']])->update($add);
            $id = $data['id'];
        }else{
            // 插入数据
            $res = BusinessListing::insertGetId($add);
            $id = $res;
        }
        if (!$res){
            return Response::faild("保存失败，请重试！");
        }
        //更新商家位置
        BusinessListing::addMemberPosition($data['longitude'],$data['latitude'],$id);
        return Response::success("保存成功！");
    }

    /**
     * 获取养老院详情
     * @param Request $request
     * @return Response
     */
    public function getInfo(Request $request){
        $data = $request->post();
        if (!isset($data['id']) || $data['id'] <= 0) { //修改
            return Response::faild("商家不存在！");
        }
        $info = BusinessListing::where(['id'=>$data['id']])->first();
        return Response::success($info);
    }


}